package com.sy.rabbitmq.quickstart;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.sy.rabbitmq.TestProperties;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * rabbitmq生产端-第一个demo
 */
public class Procuder {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.创建一个链接工厂,配置初始化参数
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(TestProperties.getIp());
        connectionFactory.setPort(TestProperties.getPort());
        connectionFactory.setVirtualHost("/");
        //2.创建链接
        Connection connection = connectionFactory.newConnection();

        //3.通过链接创建一个Channel,再通过Channel发送消息
        Channel channel = connection.createChannel();

        //4.发送消息
        for(int i =0;i<5;i++){
            String msg = "testetstetst";
            //向队列发送消息,exchange如果未指定那么走默认的exchange直接根据routingKey匹配队列名
            channel.basicPublish("","test001",null,msg.getBytes());
        }
        channel.close();
        connection.close();
    }
}
